'use strict';

clientApp.controller('ListsCtrl', function($scope, $location, model) {

  $scope.lists = model.List.query().filter('owners =', model.user.Id).order('-Timestamp');
  $scope.lists.onchange = function() {

    $scope.$apply();
    console.log($scope.lists);

  };

  $scope.createList = function() {

    if ($scope.listTitle.length > 0) {

      var list = new model.List();

      _.extend(list, {
        Title: $scope.listTitle,
        Creator: model.user.Name,
        Timestamp: Date.now()
      });

      list.$save(function(result) {

        $scope.getList(result.Id);

      });

      $scope.listTitle = '';
    }

  };

  $scope.getList = function(id) {

    $location.path('list').search({id: id});

  };

});
